﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;

public partial class admin_ServiceDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Request.QueryString["serviceID"] != null)
            {
                try
                {
                    int serviceID = Int32.Parse(Request.QueryString["serviceID"].ToString());
                    ViewState["serviceID"] = serviceID;
                    LoadServiceInfo();

                }
                catch (Exception ex)
                {

                }

            }
            else
            {
                btnUpdate.Text = "Tạo mới";
                btnDelete.Visible = false;
            }
        }
    }

    private void LoadServiceInfo() 
    {
        if (ViewState["serviceID"] != null)
        {
            int serviceID = Int32.Parse(ViewState["serviceID"].ToString());
            ServiceTableAdapter serviceDA = new ServiceTableAdapter();
            BonSai.ServiceDataTable serviceDT = serviceDA.GetServiceByID(serviceID);
            if (serviceDT.Rows.Count > 0)
            {
                BonSai.ServiceRow serviceRow = serviceDT.Rows[0] as BonSai.ServiceRow;
                txtServiceID.Text = serviceRow.ServiceID.ToString();
                if(!serviceRow.IsServiceNameNull())
                    txtServiceName.Text = serviceRow.ServiceName;
                if (!serviceRow.IsBriefDescNull())
                    txtBriefDesc.Text = serviceRow.BriefDesc;
                if (!serviceRow.IsContentNull())
                    fcContent.Value = serviceRow.Content;
                hlRepresentImage.Visible = true;
                hlRepresentImage.NavigateUrl = "Service_RepresentImage.aspx?serviceID="+serviceID;
                hlRealService.Visible = true;
                hlRealService.NavigateUrl = "ProductOfService.aspx?serviceID=" + serviceID;
                if (!serviceRow.IsRepresentImageNull()) 
                {
                    serviceRepresentImage.ImageUrl = "~/img_service/" + serviceRow.RepresentImage;
                }
            }

        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Write("ServiceManager.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
          if (!Page.IsValid)
            return;

        string briefDesc = txtBriefDesc.Text;
        string serviceName = txtServiceName.Text;
        string content = fcContent.Value;
        ServiceTableAdapter serviceDA = new ServiceTableAdapter();
        if (ViewState["serviceID"] != null)
        {
            ///UpdateNews
            int serviceID = Int32.Parse(ViewState["serviceID"].ToString());
            serviceDA.UpdateServiceByID(serviceID, serviceName, briefDesc, content);
            Response.Redirect("ServiceManage.aspx?code=2");
        }
        else
        { 
            //Add New

            //serviceDA.Insert(10, serviceName, briefDesc);

            serviceDA.InsertNewService(serviceName, briefDesc, "", content);

            Response.Redirect("ServiceManage.aspx?code = 1");
        }
        
    }
    }
